﻿Imports System.IO
Imports Builds
Imports Builds.Auxiliary

Public Class frmRobotConfig

#Region "The form control events"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click

        Me.Close()

    End Sub

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click

        TestingEmail()

    End Sub

    Private Sub frmRobotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadINI()

    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

        SaveINI()

        Me.Close()

    End Sub

    Private Sub chkModifyProjectProperties_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkModifyProjectProperties.CheckedChanged

        GroupBoxProjectProperties.Enabled = CType(sender, CheckBox).Checked

    End Sub

    Private Sub cmdCompilerFilePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCompilerFilePath.Click

        CompilerFileDialog.Filter = "VB6 Compiler File|VB6.exe"
        CompilerFileDialog.ShowDialog()
        txtCompilerFilePath.Text = CompilerFileDialog.FileName

    End Sub

    Private Sub cmdBuildsDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuildsDirectory.Click

        FolderDialog.ShowDialog()
        txtBuildsDirectory.Text = FolderDialog.SelectedPath

    End Sub

    Private Sub cmdVBPOutDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVBPOutDirectory.Click

        FolderDialog.ShowDialog()
        txtVBPOutDirectory.Text = FolderDialog.SelectedPath

    End Sub

    Private Sub txtLastWriteDate_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLastWriteDate.GotFocus
        txtLastWriteDate.SelectAll()
    End Sub

    Private Sub txtLastWriteTime_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLastWriteTime.GotFocus
        txtLastWriteTime.SelectAll()
    End Sub

#End Region

#Region "Procedures for testing email, load and the ini file storage"

    Private Sub TestingEmail()

        If txtEmailTest.Text = "" Then
            MsgBox("You must indicate a valid email address to perform the Testing of the SMTP server configuration.", MsgBoxStyle.Information, Me.Text)
            Exit Sub
        End If

        Dim oEmail As EMail = Nothing

        Try

            oEmail = New EMail()

            oEmail.SecureConnection = chkSecureConnection.Checked
            oEmail.ServerSMTP = txtServerSMTP.Text
            If txtPortSMTP.Text = "" Then
                oEmail.PortSMTP = 25
            Else
                oEmail.PortSMTP = CType(txtPortSMTP.Text, Integer)
            End If
            oEmail.UserID = txtUserID.Text
            oEmail.UserPass = txtUserPass.Text
            oEmail.UserNameSend = txtUserNameSend.Text
            oEmail.Recipients = txtEmailTest.Text
            oEmail.MessageDescription = "RobotBuilds: E-mail server testing"
            oEmail.MessageContentPlainText = "This message is a test RobotBuilds SMTP server configuration."
            oEmail.MessageContentHTMLText = "<font size=2 face=arial>This message is a test RobotBuilds SMTP server configuration.</font>"

            If oEmail.Send() Then

                MsgBox("Mail delivery is successful.", MsgBoxStyle.Information, Me.Text)

            Else

                MsgBox("Mail delivery failed. Review the specified SMTP mail settings.", MsgBoxStyle.Exclamation, Me.Text)

            End If

        Catch ex As Exception

            MsgBox("Error when you perform the testing: " & ex.Message, MsgBoxStyle.Exclamation, Me.Text)

        Finally

            If Not oEmail Is Nothing Then oEmail.Dispose()

        End Try

    End Sub


    Private Sub LoadINI()

        If File.Exists(PathINI) Then

            'Compilation settings
            txtCompilerFilePath.Text = IniGet(PathINI, "BUILDS", "CompilerFilePath")
            txtBuildsDirectory.Text = IniGet(PathINI, "BUILDS", "BuildsDirectory")
            txtVBPOutDirectory.Text = IniGet(PathINI, "BUILDS", "VBPOutDirectory")
            txtLastWriteDate.Text = IniGet(PathINI, "BUILDS", "LastWriteDate")
            txtLastWriteTime.Text = IniGet(PathINI, "BUILDS", "LastWriteTime")
            chkContinueOnError.Checked = (IniGet(PathINI, "BUILDS", "ContinueOnError") = "1")
            chkModifyProjectProperties.Checked = (IniGet(PathINI, "BUILDS", "ModifyProjectProperties") = "1")
            GroupBoxProjectProperties.Enabled = chkModifyProjectProperties.Checked
            txtVersionLegalTradeMarks.Text = IniGet(PathINI, "BUILDS", "VersionLegalTradeMarks")
            txtMajorVer.Text = IniGet(PathINI, "BUILDS", "MajorVer")
            txtMinorVer.Text = IniGet(PathINI, "BUILDS", "MinorVer")
            txtRevisionVer.Text = IniGet(PathINI, "BUILDS", "RevisionVer")

            'Server SMTP settings
            txtServerSMTP.Text = IniGet(PathINI, "EMAIL", "ServerSMTP")
            txtPortSMTP.Text = IniGet(PathINI, "EMAIL", "PortSMTP")
            txtUserID.Text = IniGet(PathINI, "EMAIL", "UserID")
            txtUserPass.Text = EncryptString(IniGet(PathINI, "EMAIL", "UserPass"), "D")
            txtUserNameSend.Text = IniGet(PathINI, "EMAIL", "UserNameSend")
            txtRespondingTo.Text = IniGet(PathINI, "EMAIL", "RespondingTo")
            chkSecureConnection.Checked = (IniGet(PathINI, "EMAIL", "SecureConnection") = "1")

            'Notify settings
            txtMessageDescription.Text = IniGet(PathINI, "NOTIFY", "MessageDescription")
            txtMessageContentWithoutErrors.Text = IniGet(PathINI, "NOTIFY", "MessageContentWithoutErrors")
            txtMessageContentWithErrors.Text = IniGet(PathINI, "NOTIFY", "MessageContentWithErrors")

        End If

    End Sub

    Private Sub SaveINI()

        If Not File.Exists(PathINI) Then

            File.Create(PathINI)

        End If

        'Compilation settings
        IniWrite(PathINI, "BUILDS", "CompilerFilePath", txtCompilerFilePath.Text)
        IniWrite(PathINI, "BUILDS", "BuildsDirectory", txtBuildsDirectory.Text)
        IniWrite(PathINI, "BUILDS", "VBPOutDirectory", txtVBPOutDirectory.Text)
        IniWrite(PathINI, "BUILDS", "LastWriteDate", txtLastWriteDate.Text)
        IniWrite(PathINI, "BUILDS", "LastWriteTime", txtLastWriteTime.Text)
        IniWrite(PathINI, "BUILDS", "ContinueOnError", IIf(chkContinueOnError.Checked, "1", "0").ToString)
        IniWrite(PathINI, "BUILDS", "ModifyProjectProperties", IIf(chkModifyProjectProperties.Checked, "1", "0").ToString)
        IniWrite(PathINI, "BUILDS", "VersionLegalTradeMarks", txtVersionLegalTradeMarks.Text)
        IniWrite(PathINI, "BUILDS", "MajorVer", txtMajorVer.Text)
        IniWrite(PathINI, "BUILDS", "MinorVer", txtMinorVer.Text)
        IniWrite(PathINI, "BUILDS", "RevisionVer", txtRevisionVer.Text)

        'Server SMTP settings
        IniWrite(PathINI, "EMAIL", "ServerSMTP", txtServerSMTP.Text)
        IniWrite(PathINI, "EMAIL", "PortSMTP", txtPortSMTP.Text)
        IniWrite(PathINI, "EMAIL", "UserID", txtUserID.Text)
        IniWrite(PathINI, "EMAIL", "UserPass", EncryptString(txtUserPass.Text, "E"))
        IniWrite(PathINI, "EMAIL", "UserNameSend", txtUserNameSend.Text)
        IniWrite(PathINI, "EMAIL", "RespondingTo", txtRespondingTo.Text)
        IniWrite(PathINI, "EMAIL", "SecureConnection", IIf(chkSecureConnection.Checked, "1", "0").ToString)

        'Notify settings
        IniWrite(PathINI, "NOTIFY", "MessageDescription", txtMessageDescription.Text)
        IniWrite(PathINI, "NOTIFY", "MessageContentWithoutErrors", txtMessageContentWithoutErrors.Text)
        IniWrite(PathINI, "NOTIFY", "MessageContentWithErrors", txtMessageContentWithErrors.Text)

    End Sub

#End Region

End Class
